/* eslint-disable */
// var adapter = require('webrtc-adapter');
// var RTCSessionDescription = window.RTCSessionDescription || window.webkitRTCSessionDescription || window.mozRTCSessionDescription;
// var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
// var RTCIceCandidate = window.RTCIceCandidate || window.webkitRTCIceCandidate || window.mozRTCIceCandidate;
function createRTCPeerConnection (conf,iceBack,errorBack) {
    conf = conf || {"iceServers":[{"url":"stun:23.21.150.121","urls":"stun:23.21.150.121"},{"url":"turn:global.turn.twilio.com:3478?transport=udp","username":"bec4bff494fe57390e1c469a76998c605d4a4ee3fab2eb14becba1fc580b4343","credential":"06Eu70ZD072h/GXjFFofqKQtXV4WrLR+nkhVqnSpJ8I=","urls":"turn:global.turn.twilio.com:3478?transport=udp"},{"url":"turn:global.turn.twilio.com:3478?transport=tcp","username":"bec4bff494fe57390e1c469a76998c605d4a4ee3fab2eb14becba1fc580b4343","credential":"06Eu70ZD072h/GXjFFofqKQtXV4WrLR+nkhVqnSpJ8I=","urls":"turn:global.turn.twilio.com:3478?transport=tcp"},{"url":"turn:global.turn.twilio.com:443?transport=tcp","username":"bec4bff494fe57390e1c469a76998c605d4a4ee3fab2eb14becba1fc580b4343","credential":"06Eu70ZD072h/GXjFFofqKQtXV4WrLR+nkhVqnSpJ8I=","urls":"turn:global.turn.twilio.com:443?transport=tcp"}]};

    var pconn = null;
    try {
        pconn = new RTCPeerConnection(conf);
            // send any ice candidates to the other peer
        pconn.onicecandidate = function (event) {
        if (event.candidate) {
            console.log('Sending ICE candidate...');
            console.log(event.candidate);
            iceBack({"ice": event.candidate});
        } else {
            console.log("End of candidates");
        }
        };
    } catch (e) {
        console.error("Failed to create PeerConnection, exception: " + e.message);
        errorBack(e);
    }
    return pconn;
}
function createOfferPeer(conf,iceBack,offerBack,errorBack){
    var peer = createRTCPeerConnection(conf,iceBack,errorBack);
    if(peer)
        peer.createOffer(function(ssdp){
            peer.setLocalDescription(ssdp);
            offerBack({"sdp": ssdp});
        }, errorBack, {'mandatory': {
            'offerToReceiveAudio':true, 
            'offerToReceiveVideo':true}
        });
    return peer;
}
function createAnswerPeer(conf,remoteSdp,iceBack,answerBack,errorBack){
    var peer = createRTCPeerConnection(conf,iceBack,errorBack);
    if(peer)
        peer.setRemoteDescription(new RTCSessionDescription(remoteSdp), function() {
            peer.createAnswer(function(ssdp){
                peer.setLocalDescription(ssdp);
                answerBack({"sdp":ssdp});
            },errorBack,{'mandatory': {
                'offerToReceiveAudio':true, 
                'offerToReceiveVideo':true}
            });
        },errorBack);
    return peer;
}
function createRTCIceCandidate({sdpMLineIndex,sdpMid,candidate})
{
    return new RTCIceCandidate({sdpMLineIndex, sdpMid, candidate});
}
function getNavigatorMedia(config){
    /**
     * 为navigator统一mediaDevieces接口
     * */
    // Older browsers might not implement mediaDevices at all, so we set an empty object first
    // Some browsers partially implement mediaDevices. We can't just assign an object
    // with getUserMedia as it would overwrite existing properties.
    // Here, we will just add the getUserMedia property if it's missing.
    (navigator.mediaDevices || (navigator.mediaDevices = {})) 
    && (navigator.mediaDevices.getUserMedia || ( navigator.mediaDevices.getUserMedia = function (constraints, successCallback, errorCallback) {
          // First get ahold of getUserMedia, if present
          var getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia)
          // Some browsers just don't implement it - return a rejected promise with an error
          // to keep a consistent interface
          if(!getUserMedia) 
              return Promise.reject(new Error('getUserMedia is not implemented in this browser'))
          // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
          return new Promise(function(successCallback, errorCallback) {
              getUserMedia.call(navigator, constraints, successCallback, errorCallback)
          })
      }));
    return navigator.mediaDevices.getUserMedia(config);
}

export {
    createRTCPeerConnection,getNavigatorMedia,createOfferPeer,createAnswerPeer
}